<?php

/**
 * uniBoard Imageset edit
 *
 * Light and fast forum solution for web 2.0-like communities.
 *
 * @package		uniBoard
 * @author		Rafał Pitoń
 * @license		http://www.gnu.org/licenses/gpl-3.0.html
 * @since		Version 0.1
 * 
 * ------------------------------------------------------------------------
 * 
 * $Date: 2010-09-15 17:35:38 +0000 (Wed, 15 Sep 2010) $
 * $Revision: 61 $
 * $Author: rafio.xudb@gmail.com $
 */

// ------------------------------------------------------------------------

class action_edit_imageset{
	
	/**
	 * Main class pointer
	 *
	 * @var object
	 */
	
	private $appCore		= NULL;
	
	/**
	 * Imageset API
	 *
	 * @var object
	 */
	
	private $api			= NULL;
	
	/**
	 * Image API
	 *
	 * @var object
	 */
	
	private $image_api		= NULL;
	
	/**
	 * Temp API
	 *
	 * @var object
	 */
	
	private $temp_api		= NULL;
	
	/**
	 * Avaiable imagesets
	 *
	 * @var array
	 */
	
	private $imagesets		= array();
	
	/**
	 * Imageset Images
	 *
	 * @var array
	 */
	
	private $images			= array();
	
	// ------------------------------------------------------------------------

	/**
	 * Initialise Action
	 *
	 * @param object $appCore
	 */
	
	function __construct( $appCore){
		
		// Store appCore pointer
		$this -> appCore = $appCore;
	
		// Start Imageset API
		$this -> api = new api_imageset( $appCore, $appCore -> _REQ( 'set'));
		
		// Did we loaded api from DB?
		if ( $this -> api -> error || !$this -> api -> data_loaded)
		{
			// Draw last error
			$appCore -> output -> error( $appCore -> lang['acp_imageset_not_found'], htmlspecialchars( $this -> api -> imageset_name));
			
			// Trow user back to images list
			new action_images( $appCore);	
		}
		else
		{
			// Handle action
			if ( $appCore -> _REQ( 'do') == 'details')
			{
				// Are we messing with Master theme?
				if ( $this -> api -> imageset_id > 1 || DEVELOPER_MODE)
				{
					// Update style?
					if ( $appCore -> _POST( 'update') == 1)
					{
						// Fill API
						$this -> api -> setImagesetName( $appCore -> _POST( 'imageset_name'));
						$this -> api -> setImagesetAuthor( $appCore -> _POST( 'imageset_author'));
						$this -> api -> setImagesetEmail( $appCore -> _POST( 'imageset_email'));
						$this -> api -> setImagesetWww( $appCore -> _POST( 'imageset_www'));
						
						// Error?
						if ( $this -> api -> error)
						{
							// Draw error messages
							$appCore -> output -> error( $this -> api -> last_error, htmlspecialchars( $this -> api -> imageset_name));
										
							// Repeat form
							$this -> drawDetailsForm();
						}
						else
						{
							// Save changes
							$this -> api -> saveData();
							
							// Create Log
							$appCore -> logAdmin( 'log_admin_changed_imageset_details', htmlspecialchars( $this -> api -> imageset_name));

							// Draw message
							$appCore -> output -> message( $appCore -> lang['acp_imageset_details_updated'], htmlspecialchars( $this -> api -> imageset_name));
							
							// Display images list
							new action_images( $appCore);
						}
					}
					else
					{	
						// Draw details form
						$this -> drawDetailsForm();
					}
				}
				else
				{
					// Draw last error
					$appCore -> output -> error( $appCore -> lang['acp_imageset_master_not_editable'], $appCore -> lang['imageset_op_edit']);
					
					// Trow user back to images list
					new action_images( $appCore);	
				}
			}
			else if ( $appCore -> _REQ( 'do') == 'images')
			{
				// Draw Images
				$this -> drawImagesForm();
			}
			else if ( $appCore -> _REQ( 'do') == 'new_image')
			{
				// Are we messing with Master theme?
				if ( $this -> api -> imageset_id > 1 || DEVELOPER_MODE)
				{
					// Create image api
					$this -> image_api = new api_imageset_image( $appCore);
					
					// Set imageset
					$this -> image_api -> setImagesetId( $this -> api -> imageset_id);
					
					// Update?
					if ( $appCore -> _POST( 'update') == 1)
					{
						// Fill api
						$this -> image_api -> setImageName( $appCore -> _POST( 'image_name'));
						$this -> image_api -> setImageWidth( $appCore -> _POST( 'image_width'));
						$this -> image_api -> setImageHeight( $appCore -> _POST( 'image_height'));
						
						// Api allright?
						if ( $this -> image_api -> error)
						{
							// Draw error
							$appCore -> output -> error( $this -> image_api -> last_error, $appCore -> lang['new_imageset_upload_form']);
							
							// Draw Image Form
							$this -> drawNewImageForm();
						}
						else
						{
							// New file upload
							$file_upl = new api_file_upload( $appCore, 'image_file_upl');
							
							// Set it
							$file_upl -> setUploadSize( 4096);
							$file_upl -> setExtensions( array( '.png', '.gif', '.png', '.jpg', '.jpeg'));
							
							// Catch upload
							$file_upl -> catchFile();
							
							// File uploaded?
							if ( $file_upl -> error)
							{
								// Draw error
								$appCore -> output -> error( $file_upl -> last_error, $appCore -> lang['new_imageset_upload_form']);
							
								// Draw Image Form
								$this -> drawNewImageForm();
							}
							else
							{
								// Name autoset?
								if ( strlen( $appCore -> _POST( 'image_file')) > 0)
								{
									// Build new file name
									$file_name = $appCore -> secureFilename( $appCore -> _POST( 'image_file')) . $file_upl -> getFileExtension();
									
									// Collision?
									if ( file_exists( STYLES_DATA_PATH . 'imageset_' . $this -> api -> imageset_id . '/' . $file_name))
									{
										// Draw error
										$appCore -> output -> error( $appCore -> lang['acp_imageset_image_file_collision'], $appCore -> lang['new_imageset_upload_form']);
										
										// Draw Image Form
										$this -> drawNewImageForm();
									}
									else
									{
										// Move file
										$file_upl -> moveFile( STYLES_DATA_PATH . 'imageset_' . $this -> api -> imageset_id . '/', $file_name);
										
										// Update api
										$this -> image_api -> setImageFile( $file_name);
										
										// Detect sizes?
										if ( strlen( $appCore -> _POST( 'image_width')) > 0 && strlen( $appCore -> _POST( 'image_height')) > 0)
										{	
											// Set custom
											$this -> image_api -> setImageWidth( $appCore -> _POST( 'image_width'));
											$this -> image_api -> setImageHeight( $appCore -> _POST( 'image_height'));
										}
										else
										{
											// Detect size
											$image_size = getimagesize( STYLES_DATA_PATH . 'imageset_' . $this -> api -> imageset_id . '/' . $file_name);
											
											// Set size from file
											$this -> image_api -> setImageWidth( $image_size[0]);
											$this -> image_api -> setImageHeight( $image_size[1]);
										}
										
										// Save whole stuff
										$this -> image_api -> saveData();
										
										// Create Log
										$appCore -> logAdmin( 'log_admin_added_imageset_item', htmlspecialchars( $this -> api -> imageset_name) . ' &raquo; ' . htmlspecialchars( $this -> image_api -> image_name));

										// Back to images browse
										$appCore -> output -> drawRedirect( $appCore -> adminLink( 'act=edit_imageset&do=images&set=' . $this -> api -> imageset_id), $appCore -> lang['acp_imageset_image_added']);
									}
								}
								else
								{
									// File name
									$file_name = $appCore -> secureFilename( $file_upl -> getFileName( false)) . $file_upl -> getFileExtension();
									
									// File exists?
									if ( file_exists( STYLES_DATA_PATH . 'imageset_' . $this -> api -> imageset_id . '/' . $file_name))
									{
										// Escape collistion
										$i = 1;
										
										// Original name
										$org_name = $file_name;
										
										// Loop
										while ( file_exists( STYLES_DATA_PATH . 'imageset_' . $this -> api -> imageset_id . '/' . $file_name))
										{
											// Build new name
											$file_name = $i . '_' . $org_name;
											
											// Increase i
											$i ++;
										}
									}
									
									// Move file
									$file_upl -> moveFile( STYLES_DATA_PATH . 'imageset_' . $this -> api -> imageset_id . '/', $file_name);
									
									// Update api
									$this -> image_api -> setImageFile( $file_name);
									
									// Detect sizes?
									if ( strlen( $appCore -> _POST( 'image_width')) > 0 && strlen( $appCore -> _POST( 'image_height')) > 0)
									{	
										// Set custom
										$this -> image_api -> setImageWidth( $appCore -> _POST( 'image_width'));
										$this -> image_api -> setImageHeight( $appCore -> _POST( 'image_height'));
									}
									else
									{
										// Detect size
										$image_size = getimagesize( STYLES_DATA_PATH . 'imageset_' . $this -> api -> imageset_id . '/' . $file_name);
										
										// Set size from file
										$this -> image_api -> setImageWidth( $image_size[0]);
										$this -> image_api -> setImageHeight( $image_size[1]);
									}
									
									// Save whole stuff
									$this -> image_api -> saveData();
									
									// Create Log
									$appCore -> logAdmin( 'log_admin_added_imageset_item', htmlspecialchars( $this -> api -> imageset_name) . ' &raquo; ' . htmlspecialchars( $this -> image_api -> image_name));

									// Back to images browse
									$appCore -> output -> drawRedirect( $appCore -> adminLink( 'act=edit_imageset&do=images&set=' . $this -> api -> imageset_id), $appCore -> lang['acp_imageset_image_added']);
								}
							}
						}
					}
					else
					{
						// Draw Image Form
						$this -> drawNewImageForm();
					}
				}
				else
				{
					// Draw last error
					$appCore -> output -> error( $appCore -> lang['acp_imageset_master_not_editable'], $appCore -> lang['imageset_op_edit']);
				}
			
			}
			else if ( $appCore -> _REQ( 'do') == 'edit_image')
			{
				// Are we messing with Master theme?
				if ( $this -> api -> imageset_id > 1 || DEVELOPER_MODE)
				{
					// Make api
					$this -> image_api = new api_imageset_image( $appCore, $appCore -> _REQ( 'img'));
					
					// Api error or not ours?
					if ( !$this -> image_api -> data_loaded || $this -> image_api -> imageset_id != $this -> api -> imageset_id)
					{
						// Draw error
						$appCore -> output -> error( $appCore -> lang['acp_imageset_image_not_found'], htmlspecialchars( $this -> api -> imageset_name));
											
						// Trow user back to images list
						$this -> drawImagesForm();
					}
					else
					{
						if ( $appCore -> _POST( 'update') == 1)
						{
							// Change image name
							$this -> image_api -> setImageName( $appCore -> _POST( 'image_name'));
							
							// Change file size?
							if ( strlen( $appCore -> _POST( 'image_width')) > 0 && strlen( $appCore -> _POST( 'image_height')) > 0)
							{	
								// Set custom
								$this -> image_api -> setImageWidth( $appCore -> _POST( 'image_width'));
								$this -> image_api -> setImageHeight( $appCore -> _POST( 'image_height'));
							}
							else
							{
								// Detect size
								$image_size = getimagesize( STYLES_DATA_PATH . 'imageset_' . $this -> api -> imageset_id . '/' . $this -> image_api -> image_file);
								
								// Set size from file
								$this -> image_api -> setImageWidth( $image_size[0]);
								$this -> image_api -> setImageHeight( $image_size[1]);
							}
							
							// Original file name
							$org_file_name = $this -> image_api -> image_file;
							
							// Change file name?
							if ( $appCore -> _POST( 'image_file') != $this -> image_api -> image_file)
							{
								// New file name is not a patch?
								$new_file_name = str_replace( '/', '', $appCore -> _POST( 'image_file'));
								$new_file_name = str_replace( '\\', '', $new_file_name);
								
								// Grab file extension
								$new_file_ext = strtolower( substr( $new_file_name, strrpos( $new_file_name, '.') + 1));
								
								// Secure file name
								$new_file_name = $appCore -> secureFilename( substr( $new_file_name, 0, strrpos( $new_file_name, '.'))) . '.' . $new_file_ext;
								
								// Extension all right?
								if ( !is_dir(  STYLES_DATA_PATH . 'imageset_' . $this -> api -> imageset_id . '/' . $new_file_name) && !file_exists( STYLES_DATA_PATH . 'imageset_' . $this -> api -> imageset_id . '/' . $new_file_name) && ( $new_file_ext == 'gif' || $new_file_ext == 'jpg' || $new_file_ext == 'jpeg' || $new_file_ext == 'png'))
								{
									// Set CORRECT name
									$this -> image_api -> setImageFile( $new_file_name);
									
									// Rename our file?
									if ( !$this -> image_api -> error)
									{
										rename( STYLES_DATA_PATH . 'imageset_' . $this -> api -> imageset_id . '/' . $org_file_name, STYLES_DATA_PATH . 'imageset_' . $this -> api -> imageset_id . '/' . $new_file_name);
									}
								}
								else
								{
									// Fake error
									$this -> image_api -> setError( $appCore -> lang['acp_imageset_image_file_incorrect']);	
									
									// Set incorrect name
									$this -> image_api -> setImageFile( $new_file_name);
								}
							}
							
							// Got error?
							if ( $this -> image_api -> error)
							{
								// Draw error
								$appCore -> output -> error( join( '<br />', $this -> image_api -> errors), $appCore -> lang['imageset_image_edit_form']);
								
								// Draw form
								$this -> drawEditImageForm();
							}
							else
							{
								// Save api
								$this -> image_api -> saveData();
									
								// Create Log
								$appCore -> logAdmin( 'log_admin_edited_imageset_item', htmlspecialchars( $this -> api -> imageset_name) . ' &raquo; ' . htmlspecialchars( $this -> image_api -> image_name));

								// Redirect us back to images list
								$appCore -> output -> drawRedirect( $appCore -> adminLink( 'act=edit_imageset&do=images&set=' . $this -> api -> imageset_id), $appCore -> lang['acp_imageset_image_edited']);
							}
						}
						else
						{
							// Draw form
							$this -> drawEditImageForm();
						}
					}	
				}
				else
				{
					// Draw last error
					$appCore -> output -> error( $appCore -> lang['acp_imageset_master_not_editable'], htmlspecialchars( $this -> api -> imageset_name));
					
					// Trow user back to images list
					$this -> drawImagesForm();
				}
			}
			else if ( $appCore -> _REQ( 'do') == 'delete_image')
			{
				// Are we messing with Master theme?
				if ( $this -> api -> imageset_id > 1 || DEVELOPER_MODE)
				{
					// Make api
					$image_api = new api_imageset_image( $appCore, $appCore -> _REQ( 'img'));
					
					// Api error or not ours?
					if ( !$image_api -> data_loaded || $image_api -> imageset_id != $this -> api -> imageset_id)
					{
						// Draw error
						$appCore -> output -> error( $appCore -> lang['acp_imageset_image_not_found'], htmlspecialchars( $this -> api -> imageset_name));
					}
					else
					{		
						// Delete image api
						$image_api -> deleteData();
							
						// Create Log
						$appCore -> logAdmin( 'log_admin_deleted_imageset_item', htmlspecialchars( $this -> api -> imageset_name) . ' &raquo; ' . htmlspecialchars( $image_api -> image_name));

						// Draw message
						$appCore -> output -> message( $appCore -> lang['acp_imageset_image_deleted'], htmlspecialchars( $this -> api -> imageset_name));
					}
				}
				else
				{
					// Draw last error
					$appCore -> output -> error( $appCore -> lang['acp_imageset_master_not_editable'], htmlspecialchars( $this -> api -> imageset_name));
				}
				
				// Trow user back to images list
				$this -> drawImagesForm();
			}
			else if ( $appCore -> _REQ( 'do') == 'copy')
			{
				// Copy Api
				$this -> temp_api = clone $this -> api;
				
				// Reset ID to 0
				$this -> temp_api -> imageset_id = 0;

				// Add "copy" to imageset name
				$this -> temp_api -> setImagesetName( $this -> temp_api -> imageset_name . $appCore -> lang['imageset_form_copy_suffix']);
				
				// Update?
				if ( $appCore -> _POST( 'update'))
				{
					// Fill API
					$this -> temp_api -> setImagesetName( $appCore -> _POST( 'imageset_name'));
					$this -> temp_api -> setImagesetAuthor( $appCore -> _POST( 'imageset_author'));
					$this -> temp_api -> setImagesetEmail( $appCore -> _POST( 'imageset_email'));
					$this -> temp_api -> setImagesetWww( $appCore -> _POST( 'imageset_www'));
					
					// Errors?
					if ( $this -> temp_api -> error)
					{
						// Draw error
						$appCore -> output -> error( $this -> temp_api -> last_error, $appCore -> lang['copy_imageset_form']);
						
						// Repeat form
						$this -> drawCopyForm();
					}
					else
					{
						// Save API copy
						$this -> temp_api -> saveData();
							
						// Create new directory
						class_files::newDir( STYLES_DATA_PATH . 'imageset_' . $this -> temp_api -> imageset_id);
						
						// New multiple insert query content
						$insert_sql = array();
						
						// Grab images
						$appCore -> db -> query( 'SELECT image_name, image_file, image_width, image_height
						FROM ' . DB_PREFIX . 'imagesets_images
						WHERE imageset_id = \'' . $this -> api -> imageset_id . '\'');
						
						// Use result
						while ( $file_result = $appCore -> db -> fetch_array())
						{
							// Add file to insert
							$insert_sql[] = '\'' . $this -> temp_api -> imageset_id . '\', \''. $appCore -> db -> strEscape( $file_result[0]) . '\', \''. $appCore -> db -> strEscape( $file_result[1]) . '\', \''. $file_result[2] . '\', \''. $file_result[3] . '\'';
							
							// Copy source file to new location
							if ( file_exists( STYLES_DATA_PATH . 'imageset_' . $this -> api -> imageset_id . '/' . $file_result[1]))
							{
								copy( STYLES_DATA_PATH . 'imageset_' . $this -> api -> imageset_id . '/' . $file_result[1], STYLES_DATA_PATH . 'imageset_' . $this -> temp_api -> imageset_id . '/' . $file_result[1]);
							}
						}
						
						// Free result
						$appCore -> db -> freeResult();
						
						// Multiple insert?
						if ( !empty( $insert_sql))
						{
							$appCore -> db -> multipleInsert( 'imageset_id, image_name, image_file, image_width, image_height', $insert_sql, 'imagesets_images');
						}
							
						// Create Log
						$appCore -> logAdmin( 'log_admin_copied_imageset', htmlspecialchars( $this -> api -> imageset_name) . ' &raquo; ' . htmlspecialchars( $this -> temp_api -> imageset_name));
						
						// Redirect member back to imagesets list
						$appCore -> output -> drawRedirect( $appCore -> adminLink( 'act=images'), $appCore -> lang['acp_imageset_copied']);	
					}
				}
				else
				{		
					// Draw copy form
					$this -> drawCopyForm();	
				}
			}
			else if ( $appCore -> _REQ( 'do') == 'export')
			{	
				// Create Log
				$appCore -> logAdmin( 'log_admin_exported_imageset', htmlspecialchars( $this -> api -> imageset_name));
				
				// Perform export
				$this -> makeExport();					
			}
			else if ( $appCore -> _REQ( 'do') == 'delete')
			{
				// Master imageset?
				if ( $this -> api -> imageset_id > 1)
				{
					// Users?
					if ( $appCore -> db -> count( 'styles', 'imageset_id = \'' . $this -> api -> imageset_id . '\'') > 0)
					{
						// Select other imagesets
						$appCore -> db -> query( 'SELECT imageset_id, imageset_name FROM ' . DB_PREFIX . 'imagesets WHERE imageset_id <> \'' . $this -> api -> imageset_id . '\'');
						
						// Reiterate Imagesets
						while ( $imageset_result = $appCore -> db -> fetch_array())
						{
							// Put imageset on list
							$this -> imagesets[$imageset_result[0]] = htmlspecialchars( $imageset_result[1]);
						}
						
						// Free result
						$appCore -> db -> freeResult();
						
						// Update?
						if ( $appCore -> _POST( 'update'))
						{
							if ( !isset( $this -> imagesets[$appCore -> _POST( 'imageset_replace')]))
							{
								// Draw error
								$appCore -> output -> error( $appCore -> lang['acp_imageset_not_found'], htmlspecialchars( $this -> api -> imageset_name));
	
								// Repeat delete form
								$this -> drawDeleteForm();	
							}
							else
							{
								// Replace imageset
								$appCore -> db -> update( array( 'imageset_id' => ((integer) $appCore -> _POST( 'imageset_replace'))), 'styles', 'imageset_id = \'' . $this -> api -> imageset_id . '\'');
								
								// Kill it
								$this -> api -> deleteData();
								
								// Create Log
								$appCore -> logAdmin( 'log_admin_deleted_imageset', htmlspecialchars( $this -> api -> imageset_name));
								
								// Redirect member back to imagesets list
								$appCore -> output -> drawRedirect( $appCore -> adminLink( 'act=images'), $appCore -> lang['acp_imageset_replaced']);
							}
						}
						else
						{
							// Draw delete form
							$this -> drawDeleteForm();
						}
					}
					else
					{
						// Kill it
						$this -> api -> deleteData();
						
						// Redirect member back to imagesets list
						$appCore -> output -> drawRedirect( $appCore -> adminLink( 'act=images'), $appCore -> lang['acp_imageset_deleted']);
					}
				}
				else
				{
					// Draw error
					$appCore -> output -> error( $appCore -> lang['acp_imageset_master_not_deletable'], htmlspecialchars( $this -> api -> imageset_name));
					
					// Trow user back to groups list
					new action_images( $appCore);
				}
			}
			else
			{
				// Draw last error
				$appCore -> output -> error( $appCore -> lang['wrong_action_code'], htmlspecialchars( $this -> api -> imageset_name));
				
				// Trow user back to groups list
				new action_images( $appCore);
			}
		}
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Draws details form
	 *
	 */
	
	function drawDetailsForm(){
		
		// Load fields templates
		$this -> appCore -> templates['form_inputs'] = new tpl_form_inputs( $this -> appCore);
		
		// Start output buffer
		ob_start();
		
		// Open form
		echo $this -> appCore -> output -> openForm( $this -> appCore -> adminLink( 'act=edit_imageset&do=details&set=' . $this -> api -> imageset_id));
		
		// Hidden inout
		echo $this -> appCore -> drawHidden( 'update', 1);
		
		// Open block
		echo $this -> appCore -> output -> openFormBlock( $this -> appCore -> lang['imageset_form_sect_basic']);

		// Draw fields
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['imageset_form_name'], $this -> appCore -> drawTextInput( 'imageset_name', $this -> api -> imageset_name));
		
		// Open new section
		echo $this -> appCore -> output -> closeFormBlock();
		echo $this -> appCore -> output -> openFormBlock( $this -> appCore -> lang['imageset_form_sect_author']);

		// Draw fields
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['imageset_form_author_name'], $this -> appCore -> drawTextInput( 'imageset_author', $this -> api -> imageset_author));
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['imageset_form_author_email'], $this -> appCore -> drawTextInput( 'imageset_email', $this -> api -> imageset_email));
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['imageset_form_author_www'], $this -> appCore -> drawTextInput( 'imageset_www', $this -> api -> imageset_www));
		
		// Draw button
		echo $this -> appCore -> output -> submitButton( $this -> appCore -> lang['save_changes']);
		
		// Close block
		echo $this -> appCore -> output -> closeFormBlock();
		
		// Close form
		echo $this -> appCore -> output -> closeForm();
			
		// Draw output
		$this -> appCore -> output -> draw( $this -> appCore -> lang['imageset_op_edit'], array(
			$this -> appCore -> adminLink( 'act=images') => $this -> appCore -> lang['acp_imagesets_list'],
			'' => htmlspecialchars( $this -> api -> imageset_name)
		), ob_get_clean());
	
	}

	// ------------------------------------------------------------------------

	/**
	 * Display Imageset contents
	 *
	 */
	
	function drawImagesForm(){
		
		// Select images from DB
		$this -> appCore -> db -> query( 'SELECT *
		FROM ' . DB_PREFIX . 'imagesets_images
		WHERE imageset_id = \'' . $this -> api -> imageset_id . '\'
		ORDER BY image_name');				
		
		// Move result to array
		while ( $image_result = $this -> appCore -> db -> fetch_assoc())
		{
			// Add item to array
			$this -> images[$image_result['image_id']] = $image_result;
		}
		
		// Free it
		$this -> appCore -> db -> freeResult();
		
		// Start output buffer
		ob_start();
		
		// Open Table
		echo $this -> appCore -> output -> openTableBlock( htmlspecialchars( $this -> api -> imageset_name));
			
		// Draw different table depending on actions avaiability
		if ( $this -> api -> imageset_id > 1 || DEVELOPER_MODE)
		{
			// Add language string
			$this -> appCore -> output -> js_vars['l_delete_item_sure'] =  $this -> appCore -> lang['delete_item_sure'];
			
			// Draw Head
			echo $this -> appCore -> output -> tableHeader( array(
				array(
					'left_float width_big',
					$this -> appCore -> lang['images_list_image_data']
				),
				array(
					'right_float width_medium',
					$this -> appCore -> lang['actions']
				),
				array(
					'right_float width_longer',
					$this -> appCore -> lang['images_list_image_file']
				)
			));
			
			// Reiterate images
			foreach ( $this -> images as $image_data)
			{
				// Draw row
				echo $this -> appCore -> output -> tableRow('
					<div class="cell left_float width_big">
						<b>' . htmlspecialchars( $image_data['image_name']) . '</b>
						<div class="item_description">' . htmlspecialchars( $image_data['image_file']) . ' (' . $image_data['image_width'] . 'x' . $image_data['image_height'] . ')</div>
					</div>
					<div class="cell left_border right_float width_medium center">
						<a href="' . $this -> appCore -> adminLink( 'act=edit_imageset&do=edit_image&set=' . $this -> api -> imageset_id . '&img=' . $image_data['image_id'], true). '">' . $this -> appCore -> output -> drawImage( 'action_edit', $this -> appCore -> lang['edit']) . '</a>
						<a href="' . $this -> appCore -> adminLink( 'act=edit_imageset&do=delete_image&set=' . $this -> api -> imageset_id . '&img=' . $image_data['image_id'], true). '" class="delete_confirm">' . $this -> appCore -> output -> drawImage( 'action_delete', $this -> appCore -> lang['delete']) . '</a>
					</div>
					<div class="cell left_border right_float width_longer">
						' . ( file_exists( STYLES_DATA_PATH . 'imageset_' . $this -> api -> imageset_id . '/' . $image_data['image_file']) ? '<img src="' . htmlspecialchars( STYLES_DATA_PATH . 'imageset_' . $this -> api -> imageset_id . '/' . $image_data['image_file']). '" ALT="" TITLE="" width="' . $image_data['image_width'] . '" height="' . $image_data['image_height'] . '" />': '') . '
					</div>');
			}
		}
		else
		{
			// Draw Head
			echo $this -> appCore -> output -> tableHeader( array(
				array(
					'left_float width_big',
					$this -> appCore -> lang['images_list_image_data']
				),
				array(
					'left_float width_longer',
					$this -> appCore -> lang['images_list_image_file']
				)
			));
			
			// Reiterate images
			foreach ( $this -> images as $image_data)
			{
				// Draw row
				echo $this -> appCore -> output -> tableRow('
					<div class="cell left_float width_big">
						<b>' . htmlspecialchars( $image_data['image_name']) . '</b>
						<div class="item_description">' . htmlspecialchars( $image_data['image_file']) . ' (' . $image_data['image_width'] . 'x' . $image_data['image_height'] . ')</div>
					</div>
					<div class="cell left_border left_float width_longer">
						' . ( file_exists( STYLES_DATA_PATH . 'imageset_' . $this -> api -> imageset_id . '/' . $image_data['image_file']) ? '<img src="' . STYLES_DATA_PATH . 'imageset_' . $this -> api -> imageset_id . '/' . $image_data['image_file'] . '" />': '') . '
					</div>');
			}
		}
				
		// Close Table
		echo $this -> appCore -> output -> closeTableBlock();
		
		// Draw imageset tools?
		if ( $this -> api -> imageset_id > 1 || DEVELOPER_MODE)
		{
			// Load fields templates
			$this -> appCore -> templates['form_inputs'] = new tpl_form_inputs( $this -> appCore);
			
			// Open Form
			echo $this -> appCore -> output -> openForm( $this -> appCore -> adminLink( 'act=edit_imageset&do=new_image&set=' . $this -> api -> imageset_id), '', true);
			echo $this -> appCore -> drawHidden( 'update', 1);
		
			// Open Block
			echo $this -> appCore -> output -> openFormBlock( $this -> appCore -> lang['new_imageset_upload_form']);
			
			// Draw 4 fields (File key, file name, file size, upload file)
			echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['add_image_form_name'], $this -> appCore -> drawTextInput( 'image_name'), $this -> appCore -> lang['add_image_form_name_expl']);
			echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['add_image_form_file'], $this -> appCore -> drawTextInput( 'image_file'), $this -> appCore -> lang['add_image_form_file_expl']);
			echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['add_image_form_sizes'], $this -> appCore -> drawTextInput( 'image_width', '', 40) . ' x ' . $this -> appCore -> drawTextInput( 'image_height', '', 40), $this -> appCore -> lang['add_image_form_sizes_expl']);
			echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['add_image_form_upload'], $this -> appCore -> drawFileUpload( 'image_file_upl'), $this -> appCore -> lang['add_image_form_upload_expl']);
			
			// Draw submit button
			echo $this -> appCore -> output -> submitButton( $this -> appCore -> lang['new_imageset_upload_form_submit']);
			
			// Close Block
			echo $this -> appCore -> output -> closeFormBlock();
			
			// Close Form
			echo $this -> appCore -> output -> closeForm();
		}
		
		// Draw output
		$this -> appCore -> output -> draw( $this -> appCore -> lang['imageset_op_browse'], array(
			$this -> appCore -> adminLink( 'act=images') => $this -> appCore -> lang['acp_imagesets_list'],
			'' => htmlspecialchars( $this -> api -> imageset_name)
		), ob_get_clean());
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Display New Image File form
	 *
	 */
	
	function drawNewImageForm(){
		
		// Start output buffer
		ob_start();
		
		// Load fields templates
		$this -> appCore -> templates['form_inputs'] = new tpl_form_inputs( $this -> appCore);
		
		// Open Form
		echo $this -> appCore -> output -> openForm( $this -> appCore -> adminLink( 'act=edit_imageset&do=new_image&set=' . $this -> api -> imageset_id), true);
		echo $this -> appCore -> drawHidden( 'update', 1);
		
		// Open Block
		echo $this -> appCore -> output -> openFormBlock( $this -> appCore -> lang['new_imageset_upload_form']);
		
		// Draw 4 fields (File key, file name, file size, upload file)
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['add_image_form_name'], $this -> appCore -> drawTextInput( 'image_name', $this -> image_api -> image_name), $this -> appCore -> lang['add_image_form_name_expl']);
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['add_image_form_file'], $this -> appCore -> drawTextInput( 'image_file', $this -> image_api -> image_file), $this -> appCore -> lang['add_image_form_file_expl']);
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['add_image_form_sizes'], $this -> appCore -> drawTextInput( 'image_width', $this -> image_api -> image_width, 40) . ' x ' . $this -> appCore -> drawTextInput( 'image_height', $this -> image_api -> image_height, 40), $this -> appCore -> lang['add_image_form_sizes_expl']);
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['add_image_form_upload'], $this -> appCore -> drawFileUpload( 'image_file_upl'), $this -> appCore -> lang['add_image_form_upload_expl']);
		
		// Draw submit button
		echo $this -> appCore -> output -> submitButton( $this -> appCore -> lang['new_imageset_upload_form_submit']);
		
		// Close Block
		echo $this -> appCore -> output -> closeFormBlock();
		
		// Close Form
		echo $this -> appCore -> output -> closeForm();
		
		// Draw output
		$this -> appCore -> output -> draw( $this -> appCore -> lang['imageset_op_browse'], array(
			$this -> appCore -> adminLink( 'act=images') => $this -> appCore -> lang['acp_imagesets_list'],
			$this -> appCore -> adminLink( 'act=edit_imageset&do=images&set=' . $this -> api -> imageset_id) => htmlspecialchars( $this -> api -> imageset_name)
		), ob_get_clean());
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Display Edit Image File form
	 *
	 */
	
	function drawEditImageForm(){
		
		// Start output buffer
		ob_start();
		
		// Load fields templates
		$this -> appCore -> templates['form_inputs'] = new tpl_form_inputs( $this -> appCore);
		
		// Open Form
		echo $this -> appCore -> output -> openForm( $this -> appCore -> adminLink( 'act=edit_imageset&do=edit_image&set=' . $this -> api -> imageset_id . '&img=' . $this -> image_api -> image_id), true);
		echo $this -> appCore -> drawHidden( 'update', 1);
		
		// Open Block
		echo $this -> appCore -> output -> openFormBlock( $this -> appCore -> lang['imageset_image_edit_form']);
		
		// Draw 4 fields (File key, file name, file size, upload file)
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['edit_image_form_preview'], ( file_exists( STYLES_DATA_PATH . 'imageset_' . $this -> api -> imageset_id . '/' . $this -> image_api -> image_file) ? '<img src="'. htmlspecialchars( STYLES_DATA_PATH . 'imageset_' . $this -> api -> imageset_id . '/' . $this -> image_api -> image_file).'" ALT="" TITLE="" width="' . $this -> image_api -> image_width . '" height="' . $this -> image_api -> image_height . '" />' : '<i>' . $this -> appCore -> lang['edit_image_form_preview_no'] . '</i>'));
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['add_image_form_name'], $this -> appCore -> drawTextInput( 'image_name', $this -> image_api -> image_name), $this -> appCore -> lang['add_image_form_name_expl']);
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['add_image_form_file'], $this -> appCore -> drawTextInput( 'image_file', $this -> image_api -> image_file), $this -> appCore -> lang['edit_image_form_file_expl']);
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['add_image_form_sizes'], $this -> appCore -> drawTextInput( 'image_width', $this -> image_api -> image_width, 40) . ' x ' . $this -> appCore -> drawTextInput( 'image_height', $this -> image_api -> image_height, 40), $this -> appCore -> lang['add_image_form_sizes_expl']);
		
		// Draw submit button
		echo $this -> appCore -> output -> submitButton( $this -> appCore -> lang['save_changes']);
		
		// Close Block
		echo $this -> appCore -> output -> closeFormBlock();
		
		// Close Form
		echo $this -> appCore -> output -> closeForm();
		
		// Draw output
		$this -> appCore -> output -> draw( $this -> appCore -> lang['imageset_op_browse'], array(
			$this -> appCore -> adminLink( 'act=images') => $this -> appCore -> lang['acp_imagesets_list'],
			$this -> appCore -> adminLink( 'act=edit_imageset&do=images&set=' . $this -> api -> imageset_id) => htmlspecialchars( $this -> image_api -> image_name)
		), ob_get_clean());
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Displays Imageset Copy form
	 *
	 */
	
	function drawCopyForm(){
		
		// Start output buffer
		ob_start();
		
		// Load fields templates
		$this -> appCore -> templates['form_inputs'] = new tpl_form_inputs( $this -> appCore);
		
		// Open Form
		echo $this -> appCore -> output -> openForm( $this -> appCore -> adminLink( 'act=edit_imageset&do=copy&set=' . $this -> api -> imageset_id, true));
		echo $this -> appCore -> drawHidden( 'update', 1);
		
		// Open Block
		echo $this -> appCore -> output -> openFormBlock( htmlspecialchars( $this -> api -> imageset_name));
		
		// Draw fields
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['imageset_form_name'], $this -> appCore -> drawTextInput( 'imageset_name', $this -> temp_api -> imageset_name));
		
		// Open new section
		echo $this -> appCore -> output -> closeFormBlock();
		echo $this -> appCore -> output -> openFormBlock( $this -> appCore -> lang['imageset_form_sect_author']);

		// Draw fields
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['imageset_form_author_name'], $this -> appCore -> drawTextInput( 'imageset_author', $this -> temp_api -> imageset_author));
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['imageset_form_author_email'], $this -> appCore -> drawTextInput( 'imageset_email', $this -> temp_api -> imageset_email));
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['imageset_form_author_www'], $this -> appCore -> drawTextInput( 'imageset_www', $this -> temp_api -> imageset_www));
		
		// Draw submit button
		echo $this -> appCore -> output -> submitButton( $this -> appCore -> lang['create_copy']);
		
		// Close Block
		echo $this -> appCore -> output -> closeFormBlock();
		
		// Close Form
		echo $this -> appCore -> output -> closeForm();
		
		// Draw output
		$this -> appCore -> output -> draw( $this -> appCore -> lang['copy_imageset_form'], array(
			$this -> appCore -> adminLink( 'act=images') => $this -> appCore -> lang['acp_imagesets_list'],
			$this -> appCore -> adminLink( 'act=edit_imageset&do=images&set=' . $this -> api -> imageset_id) => htmlspecialchars( $this -> api -> imageset_name)
		), ob_get_clean());
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Performs Imageset export
	 *
	 */
	
	function makeExport(){
		
		// Start content
		$file_content = '<?xml version="1.0" encoding="UTF-8"?>
<uniboard_imageset_export>
	<imageset_data>
		<imageset_name>' . base64_encode( $this -> api -> imageset_name) .'</imageset_name>
		<imageset_author>' . base64_encode( $this -> api -> imageset_author) .'</imageset_author>
		<imageset_email>' . base64_encode( $this -> api -> imageset_email) .'</imageset_email>
		<imageset_www>' . base64_encode( $this -> api -> imageset_www) .'</imageset_www>
	</imageset_data>
	<imageset_images>';
		
		// Select images from database and put them into export
		$this -> appCore -> db -> query( 'SELECT image_name, image_file, image_width, image_height
		FROM ' . DB_PREFIX . 'imagesets_images
		WHERE imageset_id = \'' . $this -> api -> imageset_id . '\'
		ORDER BY image_name');
		
		// Put them into files
		while ( $file_result = $this -> appCore -> db -> fetch_array())
		{
			// Add file
			$file_content .= '
		<image>
			<image_name>' . base64_encode( $file_result[0]) . '</image_name>
			<image_file>' . base64_encode( $file_result[1]) . '</image_file>
			<image_content>' . ( file_exists( STYLES_DATA_PATH . 'imageset_' . $this -> api -> imageset_id . '/' . $file_result[1]) ? base64_encode( file_get_contents( STYLES_DATA_PATH . 'imageset_' . $this -> api -> imageset_id . '/' . $file_result[1])) : '') . '</image_content>
			<image_width>' . base64_encode( $file_result[2]) . '</image_width>
			<image_height>' . base64_encode( $file_result[3]) . '</image_height>
		</image>';
		}
		
		// Free result
		$this -> appCore -> db -> freeResult();
		
		// Close content
		$file_content .= '
	</imageset_images>
</uniboard_imageset_export>';
		
		// Compress it with GZIP?
		if ( function_exists( 'gzencode'))
		{
			// Compress file with gzip
			$file_content = gzencode( $file_content, 9);
			
			// Send non-compressed XML file
			$this -> appCore -> fileHeaders( 'application/x-compressed', strlen( $file_content), $this -> appCore -> secureFilename( $this -> api -> imageset_name) . '.xml.gz');
			
			// Echo file content
			exit( $file_content);
		}
		else
		{
			// Send non-compressed XML file
			$this -> appCore -> fileHeaders( 'application/xml', strlen( $file_content), $this -> appCore -> secureFilename( $this -> api -> imageset_name) . '.xml');
			
			// Echo file content
			exit( $file_content);
		}
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Displays Imageset Delete Form
	 *
	 */
	
	function drawDeleteForm(){
		
		// Start output buffer
		ob_start();
		
		// Load fields templates
		$this -> appCore -> templates['form_inputs'] = new tpl_form_inputs( $this -> appCore);
		
		// Open Form
		echo $this -> appCore -> output -> openForm( $this -> appCore -> adminLink( 'act=edit_imageset&do=delete&set=' . $this -> api -> imageset_id, true));
		echo $this -> appCore -> drawHidden( 'update', 1);
		
		// Open Block
		echo $this -> appCore -> output -> openFormBlock( htmlspecialchars( $this -> api -> imageset_name));
		
		// Draw fields
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['imageset_form_replace'], $this -> appCore -> drawList( 'imageset_replace', $this -> imagesets, $this -> appCore -> _POST( 'imageset_replace')), $this -> appCore -> lang['imageset_form_replace_expl']);
		
		// Draw submit button
		echo $this -> appCore -> output -> submitButton( $this -> appCore -> lang['delete']);
		
		// Close Block
		echo $this -> appCore -> output -> closeFormBlock();
		
		// Close Form
		echo $this -> appCore -> output -> closeForm();
		
		// Draw output
		$this -> appCore -> output -> draw( $this -> appCore -> lang['imageset_op_delete'], array(
			$this -> appCore -> adminLink( 'act=images') => $this -> appCore -> lang['acp_imagesets_list'],
			$this -> appCore -> adminLink( 'act=edit_imageset&do=images&set=' . $this -> api -> imageset_id) => htmlspecialchars( $this -> api -> imageset_name)
		), ob_get_clean());
		
	}
		
}